package com.css.dzj.dzjportal.modules.statistics.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.common.util.ServiceUtil;
import com.css.dzj.dzjportal.common.util.http.HttpClientUtil;
import com.css.dzj.dzjportal.modules.statistics.vo.StaVo;
import com.css.dzj.dzjportal.sys.user.entity.LoginUser;
import com.google.gson.JsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 统计代办已办
 * @Author WT
 * @Date 2020/8/24
 * @Date 2020/8/24
 */
@RestController
@RequestMapping("/statistics")
@PropertySource(value = "classpath:base.properties")
public class StaController {

    @Value("${AP_URL}")
    private String apurl;
    @Value("${JCSS_URL}")
    private String jcssUrl;
    @Value("${XCJY_URL}")
    private String xcjyUrl;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private ServiceUtil serviceUtil;

    @GetMapping("/totalCount")
    public R getTotalCount() {
        try{
            StaVo staVo = new StaVo();
            List<Map<String,Object>> getSysApp = new ArrayList<>();
            getSysApp = ServiceUtil.getSysApp(request);
            if(null != getSysApp && getSysApp.size()>0){
                R r = serviceUtil.getUserInfoByRequest(request);
                LoginUser userInfo = (LoginUser) r.get("data");
                String params = JSON.toJSONString(userInfo);
                for (Map<String,Object> list :getSysApp ){
                    if(list.get("appName").equals("安评系统")){
                        //获取安平代办数量
                        String a = HttpClientUtil.doPostJson(apurl + "/todo/apCount",params);
                        if(!StringUtils.isEmpty(a)){
                            staVo.setMapApCount(JSON.parse(a).toString());
                        }else{
                            staVo.setMapApCount("0");
                        }

                        //获取安平已办数量
                        String b = HttpClientUtil.doPostJson(apurl + "/todo/apDoneCount",params);
                        if(!StringUtils.isEmpty(b)){
                            staVo.setMapApDoneCount(JSON.parse(b).toString());
                        }else {
                            staVo.setMapApDoneCount("0");
                        }

                    }
                    else{
                        staVo.setMapApCount("0");
                        staVo.setMapApDoneCount("0");
                    }
                    if(list.get("appName").equals("地震抗震防灾宣传教育系统")){
                        // 获取宣传教育代办数量
                        String d = HttpClientUtil.doPostJson(xcjyUrl + "/todo/xcjyCount",params);
                        if(!StringUtils.isEmpty(d)){
                            staVo.setMapXcjyCount(JSON.parse(d).toString());
                        }else{
                            staVo.setMapXcjyCount("0");
                        }

                        //获取宣传教育已办数量
                        String e = HttpClientUtil.doPostJson(xcjyUrl + "/todo/xcjyDoneCount",params);
                        if(!StringUtils.isEmpty(e)){
                            staVo.setMapXcjyDoneCount(JSON.parse(e).toString());
                        }else{
                            staVo.setMapXcjyDoneCount("0");
                        }
                    }else{
                        staVo.setMapXcjyCount("0");
                        staVo.setMapXcjyDoneCount("0");
                    }

                    if(list.get("appName").equals("地震监测设施监管系统")){
                        //获取监测设施代办数量
                        String c = HttpClientUtil.doPostJson(jcssUrl + "/todo/jcssCount",params);
                        if(!StringUtils.isEmpty(c)){
                            staVo.setMapJcssCount(JSON.parse(c).toString());
                        }else{
                            staVo.setMapJcssCount("0");
                        }
                    }else {
                        staVo.setMapJcssCount("0");
                    }

                }
            }
            return R.ok().put("data", staVo);
        }catch (Exception e){
            return R.error(e.getMessage());
        }
    }

}
